package student.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;

import edu.student.models.*;

public class SubjectsPage extends ActionSupport{

	private String subjectName;
	private String subjectCode;
	private int facultyID;
	private String facultyName;
	private String semesterName;
	private String isEnrolled;
	private String grade;
	
	private String rollNum;
	private ArrayList<Subject> subjects;
	private String selection = "";
	private String username="";
	private List<News> news;
	private List<Announcements> announcements;
	private String image="";
	private Timestamp lastLogin;
	private String userid="";

	public List<Announcements> getAnnouncements() {
			return announcements;
		}

		public void setAnnouncements(List<Announcements> announcements) {
			this.announcements = announcements;
		}

		public List<News> getNews() {
			return news;
		}

		public void setNews(List<News> news) {
			this.news = news;
		}
		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}
		

		public String getImage() {
			return image;
		}

		public void setImage(String image) {
			this.image = image;
		}
		public Timestamp getLastLogin() {
			return lastLogin;
		}

		public void setLastLogin(Timestamp lastLogin) {
			this.lastLogin = lastLogin;
		}
		
		public String getUserid() {
			return userid;
		}

		public void setUserid(String userid) {
			this.userid = userid;
		}


	
	public String execute(){
				
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		if (user == null) {
			user = new User();
		}
		rollNum = user.getRollNor();
		userid=user.getRollNor();
		username=user.getUsername();
		image=user.getImage();
		lastLogin=user.getLastLogin();
		news = News.getNews(" where ui.roll_No='"+userid+"' ");
		announcements=Announcements.getAnnouncements(" where ui.roll_No='"+userid+"' ");
		if(selection.equalsIgnoreCase("Show Enrolled Courses"))		
	     subjects = SubjectsPage.findAllEnrolledSubjects(rollNum);
		else
		 subjects = Subject.findAllSubjectsInDatabase(rollNum);
		return "success";
	}
	
	//Returns all subjects that the student has enrolled in.
	public static ArrayList<Subject> findAllEnrolledSubjects(String rollNum){
		ArrayList<Subject> selection = new ArrayList<Subject>();
		ResultSet result = null;
		Connection con = DB.getConnection();
		String query = "SELECT SUBJECT.SUBJECT_CODE, SUBJECT.SUBJECT_NAME, FACULTY.FACULTY_NAME, SEMESTER.SEMESTER_NAME, "
				+ "ENROLLMENT.GRADE "
				+ "FROM SUBJECT, FACULTY, SEMESTER, COURSE, ENROLLMENT " 
				+ "WHERE COURSE.SUBJECT_ID = SUBJECT.SUBJECT_ID AND COURSE.SEMESTER_ID = SEMESTER.SEMESTER_ID AND "
				+ "SUBJECT.FACULTY_ID = FACULTY.FACULTY_ID AND ENROLLMENT.SUBJECT_ID = SUBJECT.SUBJECT_ID AND "
				+ "ENROLLMENT.SEMESTER_ID = SEMESTER.SEMESTER_ID AND ENROLLMENT.ROLL_NO = '" + rollNum + "' AND "
				+ "ENROLLMENT.IS_ENROLLED = 'Y'";
		result = DB.readFromDB(query, con);
		try {
			while (result.next()) {
				Subject sub = new Subject();
				sub.setSubjectCode(result.getString("SUBJECT.SUBJECT_CODE"));
				sub.setSubjectName(result.getString("SUBJECT.SUBJECT_NAME"));
				sub.setFacultyName(result.getString("FACULTY.FACULTY_NAME"));
				sub.setSemesterName(result.getString("SEMESTER.SEMESTER_NAME"));
				sub.setGrade(result.getString("ENROLLMENT.GRADE"));
				selection.add(sub);		
			}
		} catch (SQLException e) {
	       System.out.println("Exception while reading from db"+ e);
		}
		DB.close(con);
		return selection;
	}
	
	public String getSubjectName() {
		return subjectName;
	}
	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}
	public String getSubjectCode() {
		return subjectCode;
	}
	public void setSubjectCode(String subjectCode) {
		this.subjectCode = subjectCode;
	}
	public int getFacultyID() {
		return facultyID;
	}
	public void setFacultyID(int facultyID) {
		this.facultyID = facultyID;
	}
	public String getFacultyName() {
		return facultyName;
	}
	public void setFacultyName(String facultyName) {
		this.facultyName = facultyName;
	}
	public String getSemesterName() {
		return semesterName;
	}
	public void setSemesterName(String semesterName) {
		this.semesterName = semesterName;
	}
	public String getIsEnrolled() {
		return isEnrolled;
	}
	public void setIsEnrolled(String isEnrolled) {
		this.isEnrolled = isEnrolled;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public String getRollNum() {
		return rollNum;
	}
	public void setRollNum(String rollNum) {
		this.rollNum = rollNum;
	}
	public ArrayList<Subject> getSubjects() {
		return subjects;
	}
	public void setSubjects(ArrayList<Subject> subjects) {
		this.subjects = subjects;
	}
	public String getSelection() {
		return selection;
	}
	public void setSelection(String selection) {
		this.selection = selection;
	}	
		
}
